<?php

class Item {
	
	public $id;
	public $modelNumber;
	public $url;
	public $desc;
	public $unitPrice;
	public $unitShipping;
	public $totalShipping;
	public $quantity;
	public $totalPrice;

    function Item($id, $quantity = 1) {
    	$this->setItem($id);
    	$this->setQuantity($quantity);
    }
    
    /**
     * genItemUrl - build link to product with id, $id
     */
    function genItemUrl($id) {
    	return str_replace("{{id}}", $id, ITEM_URL_FORMAT);
    }
    
    /**
     * getArray - returns an array containing all item data
     */
    function toArray() {
    	return array('id' => $this->id, 
					'url' => $this->url, 
					'desc' => $this->desc, 
					'unitPrice' => money_format(MONEY_FORMAT_STRING, $this->unitPrice), 
					'unitShipping' => money_format(MONEY_FORMAT_STRING, $this->unitShipping),
					'quantity' => $this->quantity, 
					'totalShipping' => money_format(MONEY_FORMAT_STRING, $this->totalShipping),
					'totalPrice' => money_format(MONEY_FORMAT_STRING, $this->totalPrice)
					);
    }
    
    /**
     * setItem - set variables for item
     */
    function setItem($id) {
    	global $database;
    	
    	$itemArr = $database->getItem($id);
    	$this->id = $itemArr[ITEM_ID];
    	$this->modelNumber = $itemArr[ITEM_MODEL_NUMBER];
    	$this->url = $this->genItemUrl($this->id);
    	$this->desc = $itemArr[ITEM_DESC];
    	$this->unitPrice = $itemArr[ITEM_PRICE];
    	$this->unitShipping = $itemArr[ITEM_SHIPPING];
    	$this->totalPrice = $this->unitPrice;
    	$this->totalShipping = $this->shipping;
    	$this->quantity = 1;
    }
    
    /**
     * setQuantity - sets the number of duplicate items in cart
     */
    function setQuantity($x) {
    	$this->quantity = $x;
    	$this->updateTotalPrice();
    }
    
    /**
     * updateTotalPrice() - calculates total price and shipping
     * cost based on item quantity
     */
    function updateTotalPrice() {
    	$this->totalPrice = $this->unitPrice * $this->quantity;
    	$this->totalShipping = $this->unitShipping * $this->quantity;
    }
}
?>